<?php
declare (strict_types=1);

namespace app\controller;


use app\BaseController;
use app\common\Common;
use app\common\UserService;
use app\job\Task;
use app\model\Config;
use app\model\LineOrders;
use app\model\User;
use app\model\UserVipChild;
use app\model\UserWithdraw;
use app\model\VipOrders;
use app\tool\Http;
use app\tool\Random;
use app\traits\Response;
use think\facade\Log;
use think\facade\Queue;
use Yansongda\Pay\Pay;

class NotifyController extends BaseController
{
	use Response;
	
	
	function withdrawNotify()
	{
		$params = $this->request->param();
		Log::record('提现回调');
		$appSecret = "SS6RX7d72sCGr8Yi"; // 秘钥
		$data = openssl_decrypt($params['requestBody'], 'AES-128-CBC', $appSecret, OPENSSL_ZERO_PADDING, $appSecret) ?? false;
		if ($data === false) {
			return '';
		};
		$data = str_replace("\x00", '', $data);
		$data = json_decode($data);
		Log::record($data);
		$order = UserWithdraw::where(['order_sn' => $data->outer_trade_no])->find();
		if (!$order) {
			return \think\Response::create('success');
		}
		if ($data->withdrawal_status == 'TRADE_FINISHED') {
			if ($order->status != 2) {
				return \think\Response::create('success');
			}
			$order->status = 4;
		} else {
			$order->status = 5;
			$order->reason = $data->error_msg;
		}
		$order->save();
		return \think\Response::create('success');
	}
	
	function returnNotify()
	{
		
		$paytype = $this->request->param('paytype');
		$config = config('pay');
		switch ($paytype) {
			case 'wechat':
				$pay = Pay::wechat($config);
				try {
					$res = $pay->callback();
				} catch (\Exception $e) {
					$this->writeJson(0, '验签错误');
				}
				$res = $res->resource;
				$res = $res['ciphertext'];
				$out_trade_no = $res['out_trade_no'];
				if (!isset($res['attach'])) {
					$this->writeJson(0, '没有支付类型,应该是退款回调');
				}
				$attach = $res['attach'];
				break;
			case 'alipay':
				$pay = Pay::alipay($config);
				try {
					$res = $pay->callback();
				} catch (\Exception $e) {
					$this->writeJson(0, $e->getMessage());
				}
				$out_trade_no = $res->out_trade_no;
				$attach = $res->passback_params;
				break;
			case 'balance':
				$out_trade_no = $this->request->param('out_trade_no');
				$attach = $this->request->param('attach');
				break;
			default:
				$this->writeJson(0, '支付类型错误');
		}
		
		switch ($attach) {
			case 'vip_renew':
				$order = VipOrders::where(['order_sn' => $out_trade_no, 'status' => 1])->find();
				if (!$order) {
					$this->writeJson(0, '订单不存在');
				}
				$vip_expire = Config::getFieldByName('vip_expire', 'value');
				$paytime = time();
				$order->status = 2;
				$order->paytime = $paytime;
				if ($order->user->vip_expiretime ?? 0 <= time()) {
					$order->user->vip_expiretime = strtotime("$vip_expire", $paytime);
				} else {
					$order->user->vip_expiretime = strtotime("$vip_expire", $order->user->vip_expiretime);
				}
				$order->together(['user'])->save();
				break;
			case 'vip':
				$order = VipOrders::where(['order_sn' => $out_trade_no, 'status' => 1])->find();
				if (!$order) {
					$this->writeJson(0, '订单不存在');
				}
				$vip_expire = Config::getFieldByName('vip_expire', 'value');
				
				$paytime = time();
				$order->status = 2;
				$order->paytime = $paytime;
				
				$order->user->vip_expiretime = strtotime("$vip_expire", $paytime);
				
				if ($order->vip_id == 1) {
					$level = 2;
				}
				if ($order->vip_id == 2) {
					$level = 4;
				}
				if ($order->vip_id == 3) {
					$level = 5;
				}
				$order->user->level = $level;
				$order->together(['user'])->save();
				
				$user = $order->user;
				
				//有人开通会员
				UserService::vipBonus($order->pay_amount);#开通会员  会籍分红
				if ($user->parent) {
					//如果他的上级是创客
					if ($user->parent->level == 2) {
						#########返推荐奖########
						$tuijian_amount = $order->pay_amount * Config::getFieldByName('chuangke_tuijian_chuangke', 'value') / 100; //创客推荐奖
						UserService::changeUserMoney($tuijian_amount, $user->parent->id, '同行奖');
						#########返推荐奖########
						
//						if ($user->parent->parent && $user->parent->parent->level >= 3) {
//							if ($user->parent->parent->level >= $user->parent->level) {
//								#########返卓越推荐奖########
//								if ($user->parent->parent->level == 3) {
//									$pingxing_tuijian_amount = $tuijian_amount * Config::getFieldByName('pingxing_hehuo_tuijian', 'value') / 100;
//								}
//								if ($user->parent->parent->level == 4) {
//									$pingxing_tuijian_amount = $tuijian_amount * Config::getFieldByName('pingxing_gaoji_tuijian', 'value') / 100;
//								}
//								if ($user->parent->parent->level == 5) {
//									$pingxing_tuijian_amount = $tuijian_amount * Config::getFieldByName('pingxing_zhanlue_tuijian', 'value') / 100;
//								}
//								UserService::changeUserMoney($pingxing_tuijian_amount, $user->parent->parent->id, '卓越奖');
//								#########返卓越推荐奖########
//							} else {
//								#########返越级推荐奖########
//								$surpass = $user->parent->level - $user->parent->parent->level;
//								if ($surpass == 1) {
//									//越1级
//									$tuijian_yue1_amount = $tuijian_amount * Config::getFieldByName('tuijian_yue1', 'value') / 100;
//									UserService::changeUserMoney($tuijian_yue1_amount, $user->parent->parent->id, '超越奖');
//								}
//								if ($surpass == 2) {
//									//越2级
//									$tuijian_yue2_amount = $tuijian_amount * Config::getFieldByName('tuijian_yue2', 'value') / 100;
//									UserService::changeUserMoney($tuijian_yue2_amount, $user->parent->parent->id, '超越奖');
//								}
//								#########返越级推荐奖########
//							}
//						}
						
						if ($user->parent->level < 3) {
							$jiandian_parent = Common::findParent($user->parent);
							if ($jiandian_parent) {
								#########返见点奖########
								if ($jiandian_parent->level == 3) {
									$jiandian_amount = $order->pay_amount * Config::getFieldByName('hehuo_jiandian', 'value') / 100; //联动奖
								}
								if ($jiandian_parent->level == 4) {
									$jiandian_amount = $order->pay_amount * Config::getFieldByName('gaoji_jiandian', 'value') / 100; //联动奖
								}
								if ($jiandian_parent->level == 5) {
									$jiandian_amount = $order->pay_amount * Config::getFieldByName('zhanlue_jiandian', 'value') / 100; //联动奖
								}
								UserService::changeUserMoney($jiandian_amount, $jiandian_parent->id, '联动奖');
								#########返见点奖########
								
								if ($jiandian_parent->parent && $jiandian_parent->parent->level >= 3) {
									if ($jiandian_parent->parent->level >= $jiandian_parent->level) {
										#########返卓越见点奖########
										if ($jiandian_parent->parent->level == 3) {
											$pingxing_jiandian_amount = $jiandian_amount * Config::getFieldByName('pingxing_hehuo_jiandian', 'value') / 100;
										}
										
										if ($jiandian_parent->parent->level == 4) {
											$pingxing_jiandian_amount = $jiandian_amount * Config::getFieldByName('pingxing_gaoji_jiandian', 'value') / 100;
										}
										
										if ($jiandian_parent->parent->level == 5) {
											$pingxing_jiandian_amount = $jiandian_amount * Config::getFieldByName('pingxing_zhanlue_jiandian', 'value') / 100;
										}
										UserService::changeUserMoney($pingxing_jiandian_amount, $jiandian_parent->parent->id, '卓越奖');
										#########返卓越见点奖########
									} else {
										#########返越级见点奖########
										$surpass = $jiandian_parent->level - $jiandian_parent->parent->level;
										if ($surpass == 1) {
											//越1级
											$jiandian_yue1_amount = $jiandian_amount * Config::getFieldByName('jiandian_yue1', 'value') / 100;
											UserService::changeUserMoney($jiandian_yue1_amount, $jiandian_parent->parent->id, '超越奖');
										}
										if ($surpass == 2) {
											//越2级
											$jiandian_yue2_amount = $jiandian_amount * Config::getFieldByName('jiandian_yue2', 'value') / 100;
											UserService::changeUserMoney($jiandian_yue2_amount, $jiandian_parent->parent->id, '超越奖');
										}
										#########返越级见点奖########
									}
								}
							}
						}
						
						$vip_child = UserVipChild::where(['user_id' => $user->parent->id])->column('child_id');
						
						if (count($vip_child) <= 1 && !in_array($user->id, $vip_child)) {
							//如果上级邀请的推广的创客有2个人时  升级成 合伙人
							if (count($vip_child) == 1) {
								$user->parent->level = 3;
								$time = time();
								$user->parent->vip_expiretime = strtotime("$vip_expire", $time);
								$user->parent->save();
							}
							UserVipChild::create(['user_id' => $user->parent->id, 'child_id' => $user->id]);
						}
			
					} elseif ($user->parent->level == 3) {
						//如果他的上级是合伙人
						#########返推荐奖########
						
						$tuijian_amount = $order->pay_amount * Config::getFieldByName('hehuo_tuijian', 'value') / 100; //创客推荐奖
						
						UserService::changeUserMoney($tuijian_amount, $user->parent->id, '同行奖');
						#########返推荐奖########
						if ($user->parent->parent && $user->parent->parent->level >= 3) {
							if ($user->parent->parent->level >= $user->parent->level) {
								#########返卓越推荐奖########
								if ($user->parent->parent->level == 3) {
									$pingxing_tuijian_amount = $tuijian_amount * Config::getFieldByName('pingxing_hehuo_tuijian', 'value') / 100;
								}
								
								if ($user->parent->parent->level == 4) {
									$pingxing_tuijian_amount = $tuijian_amount * Config::getFieldByName('pingxing_gaoji_tuijian', 'value') / 100;
								}
								
								if ($user->parent->parent->level == 5) {
									$pingxing_tuijian_amount = $tuijian_amount * Config::getFieldByName('pingxing_zhanlue_tuijian', 'value') / 100;
								}
								UserService::changeUserMoney($pingxing_tuijian_amount, $user->parent->parent->id, '卓越奖');
								#########返卓越推荐奖########
							} else {
								#########返越级推荐奖########
								$surpass = $user->parent->level - $user->parent->parent->level;
								if ($surpass == 1) {
									//越1级
									$tuijian_yue1_amount = $tuijian_amount * Config::getFieldByName('tuijian_yue1', 'value') / 100;
									UserService::changeUserMoney($tuijian_yue1_amount, $user->parent->parent->id, '超越奖');
								}
								if ($surpass == 2) {
									//越2级
									$tuijian_yue2_amount = $tuijian_amount * Config::getFieldByName('tuijian_yue2', 'value') / 100;
									UserService::changeUserMoney($tuijian_yue2_amount, $user->parent->parent->id, '超越奖');
								}
								#########返越级推荐奖########
							}
						}
						
						
						if ($user->parent->level < 3) {
							$jiandian_parent = Common::findParent($user->parent);
							if ($jiandian_parent) {
								#########返见点奖########
								if ($jiandian_parent->level == 3) {
									$jiandian_amount = $order->pay_amount * Config::getFieldByName('hehuo_jiandian', 'value') / 100; //联动奖
								}
								if ($jiandian_parent->level == 4) {
									$jiandian_amount = $order->pay_amount * Config::getFieldByName('gaoji_jiandian', 'value') / 100; //联动奖
								}
								if ($jiandian_parent->level == 5) {
									$jiandian_amount = $order->pay_amount * Config::getFieldByName('zhanlue_jiandian', 'value') / 100; //联动奖
								}
								UserService::changeUserMoney($jiandian_amount, $jiandian_parent->id, '联动奖');
								#########返见点奖########
								
								if ($jiandian_parent->parent && $jiandian_parent->parent->level >= 3) {
									if ($jiandian_parent->parent->level >= $jiandian_parent->level) {
										#########返卓越奖见点奖########
										if ($jiandian_parent->parent->level == 3) {
											$pingxing_jiandian_amount = $jiandian_amount * Config::getFieldByName('pingxing_hehuo_jiandian', 'value') / 100;
										}
										
										if ($jiandian_parent->parent->level == 4) {
											$pingxing_jiandian_amount = $jiandian_amount * Config::getFieldByName('pingxing_gaoji_jiandian', 'value') / 100;
										}
										
										if ($jiandian_parent->parent->level == 5) {
											$pingxing_jiandian_amount = $jiandian_amount * Config::getFieldByName('pingxing_zhanlue_jiandian', 'value') / 100;
										}
										UserService::changeUserMoney($pingxing_jiandian_amount, $jiandian_parent->parent->id, '卓越奖');
										#########返卓越奖见点奖########
									} else {
										#########返越级见点奖########
										$surpass = $jiandian_parent->level - $jiandian_parent->parent->level;
										if ($surpass == 1) {
											//越1级
											$jiandian_yue1_amount = $jiandian_amount * Config::getFieldByName('jiandian_yue1', 'value') / 100;
											UserService::changeUserMoney($jiandian_yue1_amount, $jiandian_parent->parent->id, '超越奖');
										}
										if ($surpass == 2) {
											//越2级
											$jiandian_yue2_amount = $jiandian_amount * Config::getFieldByName('jiandian_yue2', 'value') / 100;
											UserService::changeUserMoney($jiandian_yue2_amount, $jiandian_parent->parent->id, '超越奖');
										}
										#########返越级见点奖########
									}
								}
							}
						}
					} elseif (in_array($user->parent->level, [4, 5])) {
						
						#########返推荐奖########
						if ($user->parent->level == 4) {
							$tuijian_amount = $order->pay_amount * Config::getFieldByName('gaoji_tuijian', 'value') / 100; //高级推荐奖
						} else {
							$tuijian_amount = $order->pay_amount * Config::getFieldByName('zhanlue_tuijian', 'value') / 100; //战略推荐奖
						}
						UserService::changeUserMoney($tuijian_amount, $user->parent->id, '同行奖');
						#########返推荐奖########
						if ($user->parent->parent && $user->parent->parent->level >= 3) {
							
							
							if ($user->parent->parent->level >= $user->parent->level) {
								#########返卓越奖推荐奖########
								if ($user->parent->parent->level == 3) {
									$pingxing_tuijian_amount = $tuijian_amount * Config::getFieldByName('pingxing_hehuo_tuijian', 'value') / 100;
								}
								
								if ($user->parent->parent->level == 4) {
									$pingxing_tuijian_amount = $tuijian_amount * Config::getFieldByName('pingxing_gaoji_tuijian', 'value') / 100;
								}
								
								if ($user->parent->parent->level == 5) {
									$pingxing_tuijian_amount = $tuijian_amount * Config::getFieldByName('pingxing_zhanlue_tuijian', 'value') / 100;
								}
								UserService::changeUserMoney($pingxing_tuijian_amount, $user->parent->parent->id, '卓越奖');
								#########返卓越奖推荐奖########
							} else {
								#########返越级推荐奖########
								$surpass = $user->parent->level - $user->parent->parent->level;
								if ($surpass == 1) {
									//越1级
									$tuijian_yue1_amount = $tuijian_amount * Config::getFieldByName('tuijian_yue1', 'value') / 100;
									UserService::changeUserMoney($tuijian_yue1_amount, $user->parent->parent->id, '超越奖');
								}
								if ($surpass == 2) {
									//越2级
									$tuijian_yue2_amount = $tuijian_amount * Config::getFieldByName('tuijian_yue2', 'value') / 100;
									UserService::changeUserMoney($tuijian_yue2_amount, $user->parent->parent->id, '超越奖');
								}
								#########返越级推荐奖########
							}
						}
						
						if ($user->parent->level < 3) {
							$jiandian_parent = Common::findParent($user->parent);
							if ($jiandian_parent) {
								#########返见点奖########
								if ($jiandian_parent->level == 3) {
									$jiandian_amount = $order->pay_amount * Config::getFieldByName('hehuo_jiandian', 'value') / 100; //联动奖
								}
								if ($jiandian_parent->level == 4) {
									$jiandian_amount = $order->pay_amount * Config::getFieldByName('gaoji_jiandian', 'value') / 100; //联动奖
								}
								if ($jiandian_parent->level == 5) {
									$jiandian_amount = $order->pay_amount * Config::getFieldByName('zhanlue_jiandian', 'value') / 100; //联动奖
								}
								UserService::changeUserMoney($jiandian_amount, $jiandian_parent->id, '联动奖');
								#########返见点奖########
								
								if ($jiandian_parent->parent && $jiandian_parent->parent->level >= 3) {
									if ($jiandian_parent->parent->level >= $jiandian_parent->level) {
										#########返卓越见点奖########
										if ($jiandian_parent->parent->level == 3) {
											$pingxing_jiandian_amount = $jiandian_amount * Config::getFieldByName('pingxing_hehuo_jiandian', 'value') / 100;
										}
										
										if ($jiandian_parent->parent->level == 4) {
											$pingxing_jiandian_amount = $jiandian_amount * Config::getFieldByName('pingxing_gaoji_jiandian', 'value') / 100;
										}
										
										if ($jiandian_parent->parent->level == 5) {
											$pingxing_jiandian_amount = $jiandian_amount * Config::getFieldByName('pingxing_zhanlue_jiandian', 'value') / 100;
										}
										UserService::changeUserMoney($pingxing_jiandian_amount, $jiandian_parent->parent->id, '卓越奖');
										#########返卓越见点奖奖########
									} else {
										#########返越级见点奖########
										$surpass = $jiandian_parent->level - $jiandian_parent->parent->level;
										if ($surpass == 1) {
											//越1级
											$jiandian_yue1_amount = $jiandian_amount * Config::getFieldByName('jiandian_yue1', 'value') / 100;
											UserService::changeUserMoney($jiandian_yue1_amount, $jiandian_parent->parent->id, '超越奖');
										}
										if ($surpass == 2) {
											//越2级
											$jiandian_yue2_amount = $jiandian_amount * Config::getFieldByName('jiandian_yue2', 'value') / 100;
											UserService::changeUserMoney($jiandian_yue2_amount, $jiandian_parent->parent->id, '超越奖');
										}
										#########返超越奖见点奖########
									}
								}
							}
						}
					}
				}
				break;
			case 'line':
				$order = LineOrders::where(['order_sn' => $out_trade_no, 'status' => 1])->find();
				if (!$order) {
					$this->writeJson(0, '订单不存在');
				}
				$paytime = time();
				$order->status = 2;
				$order->spot->sales += 1;
				$order->paytime = $paytime;
				$order->together(['spot'])->save();
				
				Queue::later(strtotime($order->day) + 60 * 60 * 3 - time() , Task::class, ['order_id' => $order->id, 'action' => 'line_order_done'], 'task');
				
				
				$user = $order->user;
				
				//如果他的上级是创客
				if (in_array($user->parent->level, [2, 3, 4, 5])) {
					#########返佣金########
					UserService::changeUserMoney($order->spot->reward, $user->parent->id, '路线返佣');
					#########返佣金########
					if ($user->parent->parent) {
						if ($user->parent->parent->level >= $user->parent->level) {
							#########返平行佣金########
							if ($user->parent->parent->level == 3) {
								$pingxing_spot_amount = $order->spot->reward * Config::getFieldByName('pingxing_hehuo_spot', 'value') / 100;
							}
							
							if ($user->parent->parent->level == 4) {
								$pingxing_spot_amount = $order->spot->reward * Config::getFieldByName('pingxing_gaoji_spot', 'value') / 100;
							}
							
							if ($user->parent->parent->level == 5) {
								$pingxing_spot_amount = $order->spot->reward * Config::getFieldByName('pingxing_zhanlue_spot', 'value') / 100;
							}
							UserService::changeUserMoney($pingxing_spot_amount, $user->parent->parent->id, '卓越奖');
							#########返平行佣金########
						} elseif (in_array($user->parent->parent, [3, 4, 5])) {
							#########返越级佣金########
							$surpass = $user->parent->level - $user->parent->parent->level;
							if ($surpass == 1) {
								//越1级
								$spot_yue1_amount = $order->spot->reward * Config::getFieldByName('spot_yue1', 'value') / 100;
								UserService::changeUserMoney($spot_yue1_amount, $user->parent->parent->id, '超越奖');
							}
							if ($surpass == 2) {
								//越2级
								$spot_yue2_amount = $order->spot->reward * Config::getFieldByName('spot_yue2', 'value') / 100;
								UserService::changeUserMoney($spot_yue2_amount, $user->parent->parent->id, '超越奖');
							}
							#########返越级佣金########
						}
					}
				}
				
				
				break;
			default:
				$this->writeJson(0, '回调错误');
		}
		
		return match ($paytype) {
			'wechat', 'alipay' => $pay->success(),
			'balance' => $this->writeJson(),
			default => $this->writeJson(0, '支付类型错误'),
		};
		
	}
	
}
